package com.groupfifteen.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.groupfifteen.entity.SysFillQuestionEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysFillQuestionDao extends BaseMapper<SysFillQuestionEntity> {

    /**
     * 通过试卷id查询问题
     *
     * @param paperId 试卷id
     * @return 问题
     */
    @Select("select * from sys_fill_question where question_id in (select question_id from sys_paper_manage where question_type " +
            "= 2 and paper_id = #{paperId})")
    List<SysFillQuestionEntity> findByIdAndType(Integer paperId);

    /**
     * 查询最后一条填空题
     *
     * @return 最后一条填空题
     */
//    @Select("select question_id from sys_fill_question order by question_id desc limit 1")
//    SysFillQuestionEntity findOnlyQuestionId();
    @Select("select * from sys_fill_question order by question_id desc limit 1")
    SysFillQuestionEntity findOnlyQuestionId();

    /**
     * 添加填空题
     *
     * @param fillQuestion 填空题内容
     * @return 结果
     */
    @Options(useGeneratedKeys = true, keyProperty = "question_id")
    @Insert("insert into sys_fill_question(subject,question,answer,analysis,level,section) values " +
            "(#{subject},#{question},#{answer},#{analysis},#{level},#{section})")
    Integer add(SysFillQuestionEntity fillQuestion);

    /**
     * 通过学科查询
     *
     * @param subject 学科查询
     * @param pageNo  分页数
     * @return 题目
     */
    @Select("select question_id from sys_fill_question where subject = #{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(@Param("subject") String subject, @Param("pageNo") Integer pageNo);

    /**
     * 编辑填空题
     *
     * @param fillQuestion 填空题内容
     * @return 结果
     */
    @Update("update sys_fill_question set section = #{section}, question = #{question}, answer = #{answer}, level = " +
            "#{level}, analysis = #{analysis} where question_id = #{question_id}")
    Integer edit(SysFillQuestionEntity fillQuestion);
}
